package com.atguigu.tms.mock.service.impl;

import com.atguigu.mock.util.MapUtil;
import com.atguigu.mock.util.RandomNumString;
import com.atguigu.tms.mock.bean.BaseComplex;
import com.atguigu.tms.mock.bean.EmployeeInfo;
import com.atguigu.tms.mock.bean.ExpressCourier;
import com.atguigu.tms.mock.bean.ExpressCourierComplex;
import com.atguigu.tms.mock.mapper.ExpressCourierMapper;
import com.atguigu.tms.mock.service.BaseComplexService;
import com.atguigu.tms.mock.service.EmployeeInfoService;
import com.atguigu.tms.mock.service.ExpressCourierComplexService;
import com.atguigu.tms.mock.service.ExpressCourierService;
import com.atguigu.tms.mock.service.adv.impl.AdvServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 快递员表 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2022-03-20
 */
@Service
@DependsOn(value = {"employeeInfoServiceImpl","expressCourierComplexServiceImpl","baseComplexServiceImpl"})
public class ExpressCourierServiceImpl extends AdvServiceImpl<ExpressCourierMapper, ExpressCourier> implements ExpressCourierService {

    @Autowired
    ExpressCourierComplexService expressCourierComplexService;

    @Autowired
    BaseComplexService baseComplexService;

    @Autowired
    EmployeeInfoService employeeInfoService;

    @Override
    public List<ExpressCourier> initExpressCourier(List<EmployeeInfo> employeeInfoList,String expressType) {
        List<ExpressCourier> expressCourierList=new ArrayList<>(employeeInfoList.size());
        for (EmployeeInfo employeeInfo : employeeInfoList) {
            ExpressCourier expressCourier= genExpressCourier(  employeeInfo ,  expressType);
            expressCourierList.add(expressCourier);
        }
        this.saveOrUpdateBatch(expressCourierList,1000,true);

        return expressCourierList;
    }

    public ExpressCourier genExpressCourier(EmployeeInfo employeeInfo ,String expressType){
        ExpressCourier expressCourier = new ExpressCourier();
        expressCourier.setEmpId(employeeInfo.getId());
        expressCourier.setIsDeleted("0");
        expressCourier.setCreateTime(employeeInfo.getCreateTime());
        expressCourier.setExpressType(expressType);
        expressCourier.setWorkingPhone("13"+ RandomNumString.getRandNumString(1,9,9,""));

        expressCourier.setEmployeeInfo(employeeInfo);
        return expressCourier;
    }

    public Map<Long,ExpressCourier> getComplexCourierMap(String expressType){
        List<ExpressCourier> expressCourierList = baseMapper.selectExpressCourierWithComplexId(expressType);
        Map<Long, ExpressCourier> complexCourierMap = MapUtil.getIdMapFromList(expressCourierList, "complexId");
        return  complexCourierMap;
    }



    @Override
    public void loadCache(){
        cache=new ConcurrentHashMap<>();
        List<ExpressCourier> list = list();
        for (ExpressCourier expressCourier : list) {
            List<ExpressCourierComplex> expressCourierComplexList = expressCourierComplexService.list(new QueryWrapper<ExpressCourierComplex>().eq("courier_emp_id", expressCourier.getId()));
            for (ExpressCourierComplex expressCourierComplex : expressCourierComplexList) {
                BaseComplex baseComplex = baseComplexService.getById(expressCourierComplex.getComplexId(), true);
                expressCourier.getBaseComplexList().add(baseComplex);
            }
            EmployeeInfo employeeInfo = employeeInfoService.getById(expressCourier.getEmpId(), true);
            expressCourier.setEmployeeInfo(employeeInfo);
        }





        loadCache(  list);
    }


    public ExpressCourier getCourierByComplex(String expressType, Long complexId){
        ExpressCourier expressCourier = baseMapper.selectExpressCourierByComplexId(expressType, complexId);
        return  expressCourier;


    }


    protected Long getId(ExpressCourier entity ){
        Long id;
        try {

            Method method = entity.getClass().getDeclaredMethod("getEmpId");
            id = (Long)method.invoke(entity);
            return  id;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("can't get id from the bean");
        }
    }
}
